<html>
<head>
  <script src="http://openlayers.org/api/OpenLayers.js"></script>
  <script src="../../lib/OpenLayers/Control/Bookmark.js"></script>
  <script type="text/javascript">
    var map;

    function test_Control_Bookmark_constructor (t) {
        t.plan( 2 );

        var control = new OpenLayers.Control.Bookmark();
        t.ok( control instanceof OpenLayers.Control.Bookmark, "new OpenLayers.Control.Bookmark returns object" );
        t.eq( control.displayClass,  "olControlBookmark", "displayClass is correct" );
    }

    function test_Control_Bookmark_draw (t) {
        t.plan( 2 );

        var map = new OpenLayers.Map('map');
        var control = new OpenLayers.Control.Bookmark();
        map.addControl(control);

        var div = control.draw();
        t.ok( control.div != null, "draw makes a div" );
        t.ok( div != null, "draw returns its div" );
    }

    function test_Control_Bookmark_outsideViewport (t) {
        t.plan( 2 );

        var map = new OpenLayers.Map('map');
        var control = new OpenLayers.Control.Bookmark({'div':OpenLayers.Util.getElement('bookmark')});
        map.addControl(control);
        t.eq(control.div.style.width, "250px", "Div is not minimized when added.");
        control = new OpenLayers.Control.Bookmark();
        map.addControl(control);
        t.eq(control.div.style.width, "0px", "Div is minimized when added.");
    }

    function test_Control_Bookmark_loadContents(t) {
        t.plan( 5 );

        var map = new OpenLayers.Map('map');
        var control = new OpenLayers.Control.Bookmark();
        map.addControl(control);

        t.ok(control.contentDiv != null, "correctly makes content div");
        t.ok(control.dataLblDiv != null, "correctly makes data label div");
        t.ok(control.dataElementDiv != null, "correctly makes data element div");
        t.ok(control.maximizeDiv != null, "correctly makes maximize div");
        t.ok(control.minimizeDiv != null, "correctly makes minimize div");
    }

    function test_Control_Bookmark_add_remove (t) {
        t.plan( 4 );

        var map = new OpenLayers.Map('map');
        var control = new OpenLayers.Control.Bookmark();
        map.addControl(control);

        control.add( "BOSTON", new OpenLayers.Bounds(-71.401901,41.989908,-70.698776,42.693033) );
        t.ok(control.stack.length == 1, "correctly added bookmark using object");

        control.remove( "BOSTON" );
        t.ok(control.stack.length == 0, "correctly removed bookmark object");

        control.add( "BOSTON", "-71.401901,41.989908,-70.698776,42.693033" );
        t.ok(control.stack.length == 1, "correctly added bookmark using string");

        control.remove( "BOSTON" );
        t.ok(control.stack.length == 0, "correctly removed bookmark string");
    }

    function test_Control_Bookmark_find (t) {
        t.plan( 2 );

        var map = new OpenLayers.Map('map');
        var control = new OpenLayers.Control.Bookmark();
        map.addControl(control);

        control.add( "BOSTON", new OpenLayers.Bounds(-71.401901,41.989908,-70.698776,42.693033) );
        found = control.find("BOSTON");
        t.ok(found.length == 1, "correctly found single bookmark");

        found = "";
        control.add( "BOSTON", new OpenLayers.Bounds(-71.401901,41.989908,-70.698776,42.693033) );
        control.add( "BOSTON", new OpenLayers.Bounds(-71.401901,41.989908,-70.698776,42.693033) );
        found = control.find("BOSTON");
        t.ok(found.length == 3, "correctly found multiple bookmarks");
    }

    function test_Control_Bookmark_zoomToLabel (t) {
        t.plan( 1 );

        var map = new OpenLayers.Map('map');
        var layer = new OpenLayers.Layer.WMS( "OpenLayers WMS",
                   "http://labs.metacarta.com/wms/vmap0",
                   {layers: 'basic'}, {'displayInLayerSwitcher':false} );

        map.addLayer( layer );
        map.zoomToMaxExtent();

        var control = new OpenLayers.Control.Bookmark();
        map.addControl(control);

        var mapExtent = map.getExtent().toBBOX();
        control.add( "BOSTON", new OpenLayers.Bounds(-71.401901,41.989908,-70.698776,42.693033) );
        control.zoomToLabel(control.find("BOSTON")[0]);
        var currentExtent = map.getExtent().toBBOX();

        // note: the map extents may not match due to map zoom levels/resolutions. all
        // we can really tell is the extents have changed.
        t.ok(currentExtent != mapExtent, "correctly changed map extents by label");
    }

    function test_Control_Bookmark_zoomToId (t) {
        t.plan( 1 );

        var map = new OpenLayers.Map('map');
        var layer = new OpenLayers.Layer.WMS( "OpenLayers WMS",
                   "http://labs.metacarta.com/wms/vmap0",
                   {layers: 'basic'}, {'displayInLayerSwitcher':false} );

        map.addLayer( layer );
        map.zoomToMaxExtent();

        var control = new OpenLayers.Control.Bookmark();
        map.addControl(control);

        var mapExtent = map.getExtent().toBBOX();
        control.add( "BOSTON", new OpenLayers.Bounds(-71.401901,41.989908,-70.698776,42.693033) );
        control.add( "BOSTON TOO", new OpenLayers.Bounds(-71.401901,41.989908,-70.698776,42.693033) );
        control.zoomToId(1);
        var currentExtent = map.getExtent().toBBOX();

        // note: the map extents may not match due to map zoom levels/resolutions. all
        // we can really tell is the extents have changed.
        t.ok(currentExtent != mapExtent, "correctly changed map extents by id");
    }

    function test_Control_Bookmark_sortByLabel (t) {
        t.plan( 1 );

        var map = new OpenLayers.Map('map');
        var control = new OpenLayers.Control.Bookmark();
        map.addControl(control);

        control.add( "SECOND", new OpenLayers.Bounds(-71.401901,41.989908,-70.698776,42.693033) );
        control.add( "FIRST", new OpenLayers.Bounds(-71.401901,41.989908,-70.698776,42.693033) );

        control.sortStack("label");
        t.ok(control.stack[0].label != "SECOND", "correctly sorted by label");
    }

    function test_Control_Bookmark_sortById (t) {
        t.plan( 2 );

        var map = new OpenLayers.Map('map');
        var control = new OpenLayers.Control.Bookmark();
        map.addControl(control);

        control.add( "SECOND", new OpenLayers.Bounds(-71.401901,41.989908,-70.698776,42.693033) );
        control.add( "FIRST", new OpenLayers.Bounds(-71.401901,41.989908,-70.698776,42.693033) );

        control.sortStack("label");
        t.ok(control.stack[0].label != "SECOND", "correctly sorted by label");
        control.sortStack("id");
        t.ok(control.stack[0].id == 0, "correctly sorted by id");
    }

    function test_Control_Bookmark_reindex (t) {
        t.plan( 1 );

        var map = new OpenLayers.Map('map');
        var control = new OpenLayers.Control.Bookmark();
        map.addControl(control);

        control.add( "SECOND", new OpenLayers.Bounds(-71.401901,41.989908,-70.698776,42.693033) );
        control.add( "FIRST", new OpenLayers.Bounds(-71.401901,41.989908,-70.698776,42.693033) );

        control.sortStack("label");
        control.reindex();
        t.ok(control.stack[0].id == 0 && control.stack[0].label == "FIRST", "correctly reindexed stack");
    }

    function test_Control_Bookmark_clear (t) {
        t.plan( 2 );

        var map = new OpenLayers.Map('map');
        var control = new OpenLayers.Control.Bookmark();
        map.addControl(control);

        control.add( "BOSTON", new OpenLayers.Bounds(-71.401901,41.989908,-70.698776,42.693033) );
        t.ok(control.stack.length == 1, "correctly added bookmark using object");
        control.clear();
        t.ok(control.stack.length == 0, "correctly clear bookmark");
    }
  </script>
</head>
<body>
    <div id="map" style="width: 1024px; height: 512px;"/>
    <div id="bookmark" style="width:250px; height:256px;" />
</body>
</html>
